/**
 * 域名替换示例
 * 展示如何使用 sourceDomain 参数进行灵活的域名替换
 */

const axios = require('axios');

// 配置
const API_BASE_URL = 'http://localhost:3000';
const API_SECRET_KEY = 'yueqiankejiyueqiankejiyueqiankeji';

/**
 * 域名替换场景示例
 */
const domainReplacementScenarios = [
    {
        name: '场景1: 默认域名替换',
        description: '将默认的 zj.yueqianyun.com 替换为新域名',
        params: {
            sourceDomain: 'zj.yueqianyun.com',
            url: 'api.newdomain.com'
        }
    },
    {
        name: '场景2: 开发环境域名替换',
        description: '将开发环境域名替换为生产环境域名',
        params: {
            sourceDomain: 'dev.example.com',
            url: 'prod.example.com'
        }
    },
    {
        name: '场景3: 测试环境域名替换',
        description: '将测试环境域名替换为正式环境域名',
        params: {
            sourceDomain: 'test.myapp.com',
            url: 'api.myapp.com'
        }
    },
    {
        name: '场景4: 本地开发域名替换',
        description: '将本地开发域名替换为远程域名',
        params: {
            sourceDomain: 'localhost:8080',
            url: 'api.remote.com'
        }
    }
];

/**
 * 测试域名替换功能
 * @param {Object} scenario 测试场景
 */
async function testDomainReplacement(scenario) {
    console.log(`\n🧪 ${scenario.name}`);
    console.log(`📝 ${scenario.description}`);
    console.log(`🔄 ${scenario.params.sourceDomain} → ${scenario.params.url}`);
    
    try {
        const uploadData = {
            appid: 'wx1234567890abcdef',
            privateKey: 'REPLACE_WITH_REAL_PRIVATE_KEY',
            url: scenario.params.url,
            sourceDomain: scenario.params.sourceDomain,
            uniacid: 123,
            entryPagePath: 'pages/index/index',
            version: '1.0.0',
            desc: `${scenario.name} - 域名替换测试`,
            xcx_id: '8'
        };
        
        console.log('请求参数:');
        console.log(`  sourceDomain: "${uploadData.sourceDomain}"`);
        console.log(`  url: "${uploadData.url}"`);
        
        // 这里只是展示参数，不实际发送请求
        console.log('⚠️ 这是演示模式，不会实际发送请求');
        console.log('✅ 参数验证通过');
        
        return true;
    } catch (error) {
        console.error('❌ 测试失败:', error.message);
        return false;
    }
}

/**
 * 演示实际的域名替换逻辑
 */
function demonstrateDomainReplacement() {
    console.log('\n🔧 域名替换逻辑演示:');
    console.log('='.repeat(50));
    
    const sampleVendorContent = `
// 示例 vendor.js 内容
const config = {
    apiUrl: "https://zj.yueqianyun.com/api/",
    uploadUrl: "https://zj.yueqianyun.com/upload/",
    imageUrl: "https://zj.yueqianyun.com/images/"
};
    `;
    
    console.log('📄 原始 vendor.js 内容:');
    console.log(sampleVendorContent);
    
    // 模拟域名替换逻辑
    const sourceDomain = 'zj.yueqianyun.com';
    const targetDomain = 'api.newdomain.com';
    
    console.log(`🔄 执行替换: ${sourceDomain} → ${targetDomain}`);
    
    // 转义正则特殊字符
    const escapedSourceDomain = sourceDomain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    const domainRegex = new RegExp(`https://${escapedSourceDomain}/`, 'g');
    const replacedContent = sampleVendorContent.replace(domainRegex, `https://${targetDomain}/`);
    
    console.log('📄 替换后的 vendor.js 内容:');
    console.log(replacedContent);
}

/**
 * 显示curl调用示例
 */
function showCurlExamples() {
    console.log('\n📋 curl 调用示例:');
    console.log('='.repeat(50));
    
    domainReplacementScenarios.forEach((scenario, index) => {
        console.log(`\n${index + 1}. ${scenario.name}:`);
        console.log(`curl -X POST ${API_BASE_URL}/upload \\`);
        console.log(`  -H "Content-Type: application/json" \\`);
        console.log(`  -H "X-API-Key: ${API_SECRET_KEY}" \\`);
        console.log(`  -d '{`);
        console.log(`    "appid": "wx1234567890abcdef",`);
        console.log(`    "privateKey": "-----BEGIN PRIVATE KEY-----\\n...\\n-----END PRIVATE KEY-----",`);
        console.log(`    "url": "${scenario.params.url}",`);
        console.log(`    "sourceDomain": "${scenario.params.sourceDomain}",`);
        console.log(`    "uniacid": 123,`);
        console.log(`    "entryPagePath": "pages/index/index",`);
        console.log(`    "version": "1.0.0",`);
        console.log(`    "desc": "${scenario.description}",`);
        console.log(`    "xcx_id": "8"`);
        console.log(`  }'`);
    });
}

/**
 * 主函数
 */
async function main() {
    console.log('🚀 域名替换功能演示');
    console.log('='.repeat(50));
    
    console.log('\n💡 新功能特性:');
    console.log('✅ 动态指定要替换的原域名');
    console.log('✅ 支持任意域名格式的替换');
    console.log('✅ 自动转义正则特殊字符');
    console.log('✅ 提供详细的替换日志');
    
    // 测试各种域名替换场景
    for (const scenario of domainReplacementScenarios) {
        await testDomainReplacement(scenario);
    }
    
    // 演示域名替换逻辑
    demonstrateDomainReplacement();
    
    // 显示curl示例
    showCurlExamples();
    
    console.log('\n🎯 总结:');
    console.log('现在您可以通过 sourceDomain 参数灵活指定要替换的原域名了！');
    console.log('这样就不需要硬编码特定的域名，适用于各种部署环境。');
}

// 如果直接运行此脚本
if (require.main === module) {
    main().catch(console.error);
}

module.exports = {
    domainReplacementScenarios,
    testDomainReplacement,
    demonstrateDomainReplacement
}; 